﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wwtfly.BBS.BackControllers.Home;
using Wwtfly.BBS.BackControllers.Attributes;
using Wwtfly.BBS.Enums.Common;
using System.Web.Mvc;
using Wwtfly.BBS.Framework.Common;
using Wwtfly.BBS.BackModels.Models.ExpertNews;
using Wwtfly.BBS.BackModels.ViewModels.ExpertNews;
using Wwtfly.BBS.BackBLL.ExpertNews;
using Wwtfly.BBS.BackBLL.Plays;
using Wwtfly.Utility;
using Wwtfly.BBS.BackModels.Models.Common;
using Wwtfly.LoggerUtility;
using Wwtfly.BBS.BackModels;
namespace Wwtfly.BBS.BackControllers.ExpertNews
{
    /// <summary>
    /// 专题新闻
    /// </summary>
    [HasFolder("Back")]
    public class BackExpertNewsController : BackBaseController
    {
        /// <summary>
        /// 分页大小 
        /// </summary>
        private static readonly int PageSize = 15;
        // 获取列表页
        [HasPower(PowerTypeEnum.Browse)]
        public ActionResult Index(string pageParams)
        {
            ViewData["Title"] = "专家专题列表页";
            PageParams pp = !string.IsNullOrEmpty(pageParams) ? new PageParams(pageParams) :
                new PageParams() { PageIndex = 1, PageSize = PageSize };
            ExpertNewsShowsSearchModel search = this.RestoreSearch<ExpertNewsShowsSearchModel>(null); 

            ExpertNewsShowsViewModel viewModel = new ExpertNewsShowsViewModel()
            {
                PageParams = pp,
                ExpertNewsShowsPages = ExpertNewsBLL.GetExpertNewsShowseList(pp, search),
                ExpertNewsShowsSearch=search??new ExpertNewsShowsSearchModel(),
                PlaysTypeList = new SelectList(PlaysTypesBLL.GetPlaysTypesList(), "LotteryID", "LotteryName")
            };

            return View(viewModel);
        }
        //浏览
        [HasPower(PowerTypeEnum.Browse)]
        public ActionResult Details(string id)
        {

            return RedirectToAction("Index");
        }

        // 搜索
        [HttpPost]
        [HasPower(PowerTypeEnum.Browse)]
        public ActionResult Search(ExpertNewsShowsViewModel viewModel)
        {
            this.SaveSearch(viewModel.ExpertNewsShowsSearch, null);
            return RedirectToAction("Index");
        }
        //重置 搜索..
        public ActionResult SearchReset()
        {
            this.ResetSearch(null);
            return RedirectToAction("Index");
        }

        // 删除
        [HasPower(PowerTypeEnum.Delete)]
        public ActionResult Delete(string id)
        {
            try
            {
                bool b = ExpertNewsBLL.Delete(id.AsInt(0));
                if (b)
                {
                    return this.Json(
                        new
                    JsonResultModel
                        {
                            ResultCode = "100",
                            ResultMessage = null
                        }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                LoggerManage.Error(new LogMessageParams()
                {
                    ModuleID = "BackExpertNewsController.cs",
                    FuncCode = "Delete(string id)",
                    Level = AppErrorLevel.EROR,
                    ErrCode = "0x0",
                    Excep = ex,
                    logMessage = "删除(专题).出错",
                    ProgramName = "Wwtilfy.BBS"
                });
            }

            return this.Json(new
            JsonResultModel
            {
                ResultCode = "-100",
                ResultMessage = string.Format("删除失败!")
            }, JsonRequestBehavior.AllowGet);
        }
        // 编辑
        [HasPower(PowerTypeEnum.Edit)]
        [ValidateInput(false)]
        public ActionResult Update(string id)
        {
            ViewData["Title"] = "修改";
            return Edit(id);
        }

        /// <summary>
        /// 返回 新增,编辑 界面
        /// </summary>
        /// <returns></returns>
        private ActionResult Edit(string id)
        {
            ExpertNewsShowsViewModel viewModel = new ExpertNewsShowsViewModel()
            {
                ExpertNewsShows = ExpertNewsBLL.GetInfo(id.AsInt(0)),
            };
            return View("Edit", viewModel);
        }
        [HttpPost, HasPower(PowerTypeEnum.Edit)]
        [ValidateInput(false)]
        public ActionResult Update(ExpertNewsShowsViewModel viewModel)
        {
            return EditSave(viewModel);
        }


        /// <summary>
        /// 编辑保存
        /// </summary>
        /// <returns></returns>
        private ActionResult EditSave(ExpertNewsShowsViewModel viewModel)
        {

            //验证...
            List<string> errMessageList = this.ValidateData(viewModel);
            if (errMessageList != null && errMessageList.Count > 0)
            {
                ErrorViewModel errorViewModel = new ErrorViewModel()
                {
                    InfoList = errMessageList
                };
                return this.ErrorResult(errorViewModel);
            }

            try
            {
                if (viewModel.ExpertNewsShows.Id >0)
                {
                    ExpertNewsShowsModel model = ExpertNewsBLL.GetInfo(viewModel.ExpertNewsShows.Id);
                    model.Extitle = viewModel.ExpertNewsShows.Extitle;
                    model.Keyword = viewModel.ExpertNewsShows.Keyword;
                    model.Exmiaoshu = viewModel.ExpertNewsShows.Exmiaoshu;
                    model.Uptime = viewModel.ExpertNewsShows.Uptime;
                    model.Endtime = viewModel.ExpertNewsShows.Endtime;
                    model.Scheme = viewModel.ExpertNewsShows.Scheme;
                    model.ColourMoney = viewModel.ExpertNewsShows.TotalCount;
                    model.ClickSum = viewModel.ExpertNewsShows.ClickSum;
                    model.CheckMsg = viewModel.ExpertNewsShows.CheckMsg;
                    model.ExperRemark = viewModel.ExpertNewsShows.ExperRemark;
                    //model.LastModifyTime = DateTime.Now;
                    //model.LastModifyUser = User.UserInfo.Id;
                    ExpertNewsBLL.Update(model);
                }
                return this.Json(new
                {
                    result = "100",
                    html = string.Format("{0}", "保存成功!")
                });
            }
            catch (Exception ex)
            {
                LoggerManage.Error(new LogMessageParams()
                {
                    ModuleID = "BackExpertNewsController.cs",
                    FuncCode = "EditSave(ExpertNewsShowsModel viewModel)",
                    Level = AppErrorLevel.EROR,
                    ErrCode = "0x0",
                    Excep = ex,
                    logMessage = "编辑保存model(专题).出错",
                    ProgramName = "Wwtilfy.BBS"
                });
            }

            return this.Json(new
            {
                result = "-100",
                title = "警告信息",
                html = string.Format("请尝试刷新页面或联系管理员.")
            });
        }

        /// <summary>
        /// 验证 相关...
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        private List<string> ValidateData(ExpertNewsShowsViewModel viewModel)
        {

            List<string> errMessageList = new List<string>();
            if (string.IsNullOrEmpty(viewModel.ExpertNewsShows.Extitle))
            {
                errMessageList.Add("专题标题不能为空!");
            }
            return errMessageList;
        }
    }
}
